//
//  SYNavigationBar.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 16/8/18.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SYNavigationBar.h"
#import "PJUltraThinLine.h"

@class PJUltraThinLine;
@implementation SYNavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;
        self.clipsToBounds = NO;
        self.autoresizesSubviews = YES;
        
        [self setUpView];
    }
    return self;
}

- (void)setUpView
{
    UIImageView *bgimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SafeAreaTopHeight)];
    [bgimg setImage:[UIImage imageNamed:@"navBar_BG"]];
    
    _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    _backgroundView.clipsToBounds = NO;
    _backgroundView.autoresizesSubviews = YES;
    _backgroundView.userInteractionEnabled = YES;
    _backgroundView.autoresizingMask = ~UIViewAutoresizingNone;
    [_backgroundView addSubview:bgimg];
    [self addSubview:_backgroundView];
    
    _navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight - 44, CGRectGetWidth(_backgroundView.frame), 44)];
    _navigationBar.userInteractionEnabled = YES;
    _navigationBar.shadowImage = [UIImage new];
    
    
    [_navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    if (iOS7_or_Later) {
        _navigationBar.barTintColor = [UIColor clearColor];
        _navigationBar.tintColor = RGBCOLOR(59, 59, 59);
        
        [_navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName: [UIColor whiteColor]}];
    }else {
//        [_navigationBar setTitleTextAttributes:@{UITextAttributeTextColor : [UIColor blackColor], UITextAttributeFont : [UIFont systemFontOfSize:18], UITextAttributeTextShadowColor: [UIColor clearColor]}];
    }
    
    [_backgroundView addSubview:_navigationBar];
    
    _navigationItem = [[UINavigationItem alloc] init];
    [_navigationBar setItems:@[_navigationItem]];
    
    _bottomLine = [[PJUltraThinLine alloc] initWithFrame:CGRectMake(0, _backgroundView.height-0.5, _backgroundView.width, 0.5)];
    _bottomLine.lineColor = [UIColor blackColor];
    _bottomLine.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    //[_backgroundView addSubview:_bottomLine];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //self.bottomLine.lineColor = NavBarLightGrayColor;
    self.bottomLine.lineColor = [UIColor colorWithHexString:@"#B2B2B2"];
    //[_backgroundView bringSubviewToFront:_bottomLine];
}

- (void)setBackgroundColor:(UIColor *)color
{
    if (!color) {
        return;
    }
    _backgroundView.backgroundColor = color;
}

@end
